package com.prs.crm.action.pm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.pm.PmSpecialty;
import com.prs.crm.service.pm.PmSpecialtyService;
import com.prs.crm.service.sys.LogService;

public class PmSpecialtyAction extends BaseAction {
	private PmSpecialty pmSpecialty;
	private PmSpecialtyService pmSpecialtyService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listPmSpecialty")
	public String listPmSpecialty() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PmSpecialty.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmSpecialtyService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editPmSpecialty")
	public String editPmSpecialty() {
		return SUCCESS;
	}

	@Action(value = "submitPmSpecialty", results = {
			@Result(name = "success", type = "redirect", location = "listPmSpecialty"),
			@Result(name = "error", type = "chain", location = "editPmSpecialty") })
	public String submitPmSpecialty() {
		if (pmSpecialty.getName() == null
				|| pmSpecialty.getName().trim().equals("")) {
			this.addActionError("请输入专业名称");
			return ERROR;
		}
		if (this.getLogService().getSearchName(PmSpecialty.class,
				pmSpecialty.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getPmSpecialtyService().save(pmSpecialty);
		return SUCCESS;
	}

	@Action(value = "removePmSpecialty", results = {
			@Result(name = "success", type = "redirect", location = "listPmSpecialty"),
			@Result(name = "error", type = "chain", location = "listPmSpecialty") })
	public String removePmSpecialty() {
		if (ids == null) {
			this.addActionError("请选择要删除的专业");
			return ERROR;
		}
		for (Integer id : ids) {
			PmSpecialty specialty = this.getPmSpecialtyService().get(id);
			if (specialty.getGroups().size() != 0) {
				this.addActionError("已有项目组引用，不能删除");
				return ERROR;
			}
		}
		this.getPmSpecialtyService().batchRemove(ids);
		return SUCCESS;
	}

	public PmSpecialty getPmSpecialty() {
		return pmSpecialty;
	}

	public void setPmSpecialty(PmSpecialty pmSpecialty) {
		this.pmSpecialty = pmSpecialty;
	}

	public PmSpecialtyService getPmSpecialtyService() {
		return pmSpecialtyService;
	}

	public void setPmSpecialtyService(PmSpecialtyService pmSpecialtyService) {
		this.pmSpecialtyService = pmSpecialtyService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
